package cn.yunyichina.provider.hymed.util;

import com.neusoft.infoSec.priEnc.PriEnc;
import com.neusoft.infoSec.pubEnc.PubEnc;
import java.util.Map;

/**
 * Created by kevin on 2017/11/24.
 */
public class RsaUtil {

    /**
     * 加密
     * @param encryptStr 需要加密的字符串
     * @param publicKey  加密的Xml
     * @return
     * @throws Exception
     */
    public static String encryptByPublicKey(String encryptStr, String publicKey)
            throws Exception {
        PubEnc pubEnc = new PubEnc();
        Map myMap = pubEnc.enString(publicKey, encryptStr);
        String appCode = (String) myMap.get("appCode");
        String errMsg = (String) myMap.get("errMsg");
        if(!"1".equals(appCode)){
            throw new RuntimeException("RsaUtil.encryptByPublicKey encrypt error->"+errMsg);
        }
        return (String) myMap.get("enString");
    }

    /**
     * 解密
     * @param decryptStr 解密字符串
     * @param privateKey 私钥
     * @return
     * @throws Exception
     */
    public static String decryptByPrivateKey(String decryptStr, String privateKey)
            throws Exception {
        PriEnc priEnc = new PriEnc();
        Map myMap = priEnc.deString(privateKey, decryptStr);
        String appCode = (String) myMap.get("appCode");
        String errMsg = (String) myMap.get("errMsg");
        if(!"1".equals(appCode)){
            throw new RuntimeException("RsaUtil.decryptByPrivateKey decrypt error->"+errMsg);
        }
        return (String)myMap.get("deString");
    }
}
